#!/bin/sh

help ()
{
  echo "Find the installed plugin/feature version of a eclipse build environment"
  echo "Usage: $0 [pluginId1/featureId1[,pluginId2/featureId2]...]";
  echo ""
  echo "Options:"
  echo "pluginId/featureId - comma seperated plugin or feature ids, empty for all"
  echo ""
  echo "Example: $0 org.eclipse.tcf.feature.group";
  exit 1;
}

fail ()
{
  local retval=$1
  shift $1
  echo "[Fail $retval]: $*"
  echo "BUILD_TOP=${BUILD_TOP}"
  cd ${TOP}
  exit ${retval}
}

find_eclipse_base ()
{
  [ -d ${ECLIPSE_HOME}/plugins ] &&  ECLIPSE_BASE=`readlink -f ${ECLIPSE_HOME}`
}

find_launcher ()
{
  local list="`ls ${ECLIPSE_BASE}/plugins/org.eclipse.equinox.launcher_*.jar`"
  for launcher in $list; do
    [ -f $launcher ] && LAUNCHER=${launcher}
  done
}

check_env ()
{
  find_eclipse_base
  find_launcher
  
  local err=0
  [ "x${ECLIPSE_BASE}" = "x" -o "x${LAUNCHER}" = "x" ] && err=1
  if [ $err -eq 0 ]; then
    [ ! -d ${ECLIPSE_BASE} ] && err=1
    [ ! -f ${LAUNCHER} ] && err=1
  fi
  
  if [ $err -ne 0 ]; then
    echo "Please set env variable ECLIPSE_HOME to the eclipse installation directory!" 
    exit 1
  fi 
}

if [ $# -ne 0 ] && [ $# -ne 1 ]; then 
   help
fi

check_env

java -jar ${LAUNCHER} -application org.eclipse.equinox.p2.director -destination ${ECLIPSE_BASE} -profile SDKProfile -repository file:///${ECLIPSE_BASE}/p2/org.eclipse.equinox.p2.engine/profileRegistry/SDKProfile.profile -list $@
